using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using AleaClasses;
using EnablementManager;

namespace AleaDesigner
{
	/// <summary>
	/// Summary description for TfrmLabels.
	/// </summary>
	public class TfrmLabels : TfrmBaseform
	{
		private System.Windows.Forms.ListView lvLabels;
		private System.Windows.Forms.ColumnHeader lblLabel;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Panel pnlLabel;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox edDisplayName;
		private System.Windows.Forms.TextBox edDescription;
		private System.Windows.Forms.Label lblLabelCode;
		private System.Windows.Forms.ContextMenu pmenLabelList;
		private System.Windows.Forms.MenuItem mniAdd;
		private System.Windows.Forms.MenuItem mniDel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public TfrmLabels()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lvLabels = new System.Windows.Forms.ListView();
			this.lblLabel = new System.Windows.Forms.ColumnHeader();
			this.pmenLabelList = new System.Windows.Forms.ContextMenu();
			this.mniAdd = new System.Windows.Forms.MenuItem();
			this.mniDel = new System.Windows.Forms.MenuItem();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.pnlLabel = new System.Windows.Forms.Panel();
			this.lblLabelCode = new System.Windows.Forms.Label();
			this.edDescription = new System.Windows.Forms.TextBox();
			this.edDisplayName = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.pnlLabel.SuspendLayout();
			this.SuspendLayout();
			// 
			// lvLabels
			// 
			this.lvLabels.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					   this.lblLabel});
			this.lvLabels.ContextMenu = this.pmenLabelList;
			this.lvLabels.Dock = System.Windows.Forms.DockStyle.Left;
			this.lvLabels.HideSelection = false;
			this.lvLabels.Location = new System.Drawing.Point(0, 0);
			this.lvLabels.MultiSelect = false;
			this.lvLabels.Name = "lvLabels";
			this.lvLabels.Size = new System.Drawing.Size(121, 273);
			this.lvLabels.TabIndex = 0;
			this.lvLabels.View = System.Windows.Forms.View.Details;
			this.lvLabels.Resize += new System.EventHandler(this.lvLabels_Resize);
			this.lvLabels.SelectedIndexChanged += new System.EventHandler(this.lvLabels_SelectedIndexChanged);
			// 
			// lblLabel
			// 
			this.lblLabel.Text = "Label";
			this.lblLabel.Width = 100;
			// 
			// pmenLabelList
			// 
			this.pmenLabelList.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						  this.mniAdd,
																						  this.mniDel});
			this.pmenLabelList.Popup += new System.EventHandler(this.pmenLabelList_Popup);
			// 
			// mniAdd
			// 
			this.mniAdd.Index = 0;
			this.mniAdd.Text = "Add Label";
			// 
			// mniDel
			// 
			this.mniDel.Index = 1;
			this.mniDel.Text = "Delete Label";
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(121, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 273);
			this.splitter1.TabIndex = 1;
			this.splitter1.TabStop = false;
			// 
			// pnlLabel
			// 
			this.pnlLabel.Controls.Add(this.lblLabelCode);
			this.pnlLabel.Controls.Add(this.edDescription);
			this.pnlLabel.Controls.Add(this.edDisplayName);
			this.pnlLabel.Controls.Add(this.label3);
			this.pnlLabel.Controls.Add(this.label2);
			this.pnlLabel.Controls.Add(this.label1);
			this.pnlLabel.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlLabel.Location = new System.Drawing.Point(124, 0);
			this.pnlLabel.Name = "pnlLabel";
			this.pnlLabel.Size = new System.Drawing.Size(324, 273);
			this.pnlLabel.TabIndex = 2;
			// 
			// lblLabelCode
			// 
			this.lblLabelCode.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lblLabelCode.Location = new System.Drawing.Point(56, 16);
			this.lblLabelCode.Name = "lblLabelCode";
			this.lblLabelCode.Size = new System.Drawing.Size(256, 23);
			this.lblLabelCode.TabIndex = 5;
			// 
			// edDescription
			// 
			this.edDescription.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.edDescription.Location = new System.Drawing.Point(8, 88);
			this.edDescription.Multiline = true;
			this.edDescription.Name = "edDescription";
			this.edDescription.Size = new System.Drawing.Size(304, 176);
			this.edDescription.TabIndex = 4;
			this.edDescription.Text = "";
			this.edDescription.TextChanged += new System.EventHandler(this.edDescription_TextChanged);
			// 
			// edDisplayName
			// 
			this.edDisplayName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.edDisplayName.Location = new System.Drawing.Point(56, 40);
			this.edDisplayName.Name = "edDisplayName";
			this.edDisplayName.Size = new System.Drawing.Size(256, 20);
			this.edDisplayName.TabIndex = 3;
			this.edDisplayName.Text = "";
			this.edDisplayName.Validating += new System.ComponentModel.CancelEventHandler(this.edDisplayName_Validating);
			this.edDisplayName.TextChanged += new System.EventHandler(this.edDisplayName_TextChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 64);
			this.label3.Name = "label3";
			this.label3.TabIndex = 2;
			this.label3.Text = "Description";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 23);
			this.label2.TabIndex = 1;
			this.label2.Text = "Name";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Code";
			// 
			// TfrmLabels
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(448, 273);
			this.Controls.Add(this.pnlLabel);
			this.Controls.Add(this.splitter1);
			this.Controls.Add(this.lvLabels);
			this.Name = "TfrmLabels";
			this.Text = "Labels";
			this.Load += new System.EventHandler(this.TfrmLabels_Load);
			this.Closed += new System.EventHandler(this.TfrmLabels_Closed);
			this.pnlLabel.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private int fIsUpdating = 0;

		public TAleaGraph AleaGraph = null;
		public TSignalChangeDelegate SignalChangeDelegate = null;
	
		private void edDisplayName_TextChanged(object sender, System.EventArgs e)
		{
			if ((fIsUpdating == 0) && SelectedLabel != null)
			{
				SelectedLabel.DisplayName = edDisplayName.Text;
				SignalChangeDelegate();
				UpdateCaptions();
			}
		}

		public TLabel SelectedLabel
		{
			get
			{
				TLabel retval = null;
				if (lvLabels.SelectedItems.Count > 0)
					retval = lvLabels.SelectedItems[0].Tag as TLabel;
				return retval;
			}
		}

		public  void UpdateControls()
		{
			fIsUpdating++;
			try
			{
				// save selection for later restoration
				TLabel SaveLabel = SelectedLabel;

				lvLabels.BeginUpdate();
				try
				{
					lvLabels.Items.Clear();

					if (AleaGraph != null)
					{
						foreach(TLabel Label in AleaGraph.Labels)
						{
							ListViewItem lListViewItem = new ListViewItem();

							lListViewItem.Text = ""; // call updatecaptions() later
							lListViewItem.Tag = Label;
							lvLabels.Items.Add(lListViewItem);

							if (SaveLabel == Label)
								lListViewItem.Selected = true;
						}
					}

					if ((lvLabels.Items.Count > 0) && (lvLabels.SelectedItems.Count == 0))
					{
						lvLabels.Items[0].Selected = true;
					}

					UpdateCaptions();
				}
				finally
				{
					lvLabels.EndUpdate();
				}
			}
			finally
			{
				fIsUpdating--;
			}
		}

		private void UpdateCaptions()
		{
			foreach(ListViewItem lvi in lvLabels.Items)
			{
				if (lvi.Tag is TLabel)
				{
					TLabel Label = lvi.Tag as TLabel;
					lvi.Text = Label.DisplayName;
					lvi.SubItems.Add(Label.DisplayName);
				}
			}
		}

		private void UpdateDetail()
		{
			fIsUpdating ++;
			try
			{
				if (SelectedLabel != null)
				{
					lblLabelCode.Text = SelectedLabel.LabelCode;
					edDisplayName.Text = SelectedLabel.DisplayName;
					edDescription.Text = SelectedLabel.Description;
				}
			}
			finally
			{
				fIsUpdating--;
			}
		}

		private void TfrmLabels_Load(object sender, System.EventArgs e)
		{
			SetupEnablements();

			UpdateControls();
		}

		private void lvLabels_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			UpdateDetail();
		}

		private void edDisplayName_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = edDisplayName.Text.Trim().Length == 0;
		}

		private void edDescription_TextChanged(object sender, System.EventArgs e)
		{
			if ((fIsUpdating == 0) && SelectedLabel != null)
			{
				SelectedLabel.Description = edDescription.Text;
				SignalChangeDelegate();
			}
		}
		
		public void DoAddLabel()
		{
			TLabel lLabel = new TLabel("New Label", "");
			AleaGraph.Labels.Add(lLabel);
			UpdateControls();
			SignalChangeDelegate();
		}

		public void DoDeleteLabel()
		{
			if (SelectedLabel != null)
			{
				if (MessageBox.Show("Are you sure?", "Delete Label", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					AleaGraph.Labels.Remove(SelectedLabel);
					UpdateControls();
					SignalChangeDelegate();
				}
			}
		}

		private void lvLabels_Resize(object sender, System.EventArgs e)
		{
			lvLabels.Columns[0].Width = lvLabels.ClientRectangle.Width;
		}

		public void SetupEnablements()
		{
			EnablementManager.SetDelegates(
				"Add Label", 
				new TEnablementCheckDelegate(AddLabelEnablementCheck),
				new TEnablementExecuteDelegate(DoAddLabel)
				);

			EnablementManager.SetDelegates(
				"Delete Label", 
				new TEnablementCheckDelegate(DeleteLabelEnablementCheck),
				new TEnablementExecuteDelegate(DoDeleteLabel)
				);

			EnablementManager.AddContextMenu(pmenLabelList);
		}

		public void TeardownEnablements()
		{
			EnablementManager.RemoveContextMenu(pmenLabelList);

			EnablementManager.ClearDelegates("Add Label");
			EnablementManager.ClearDelegates("Delete Label");
		}

		private void AddLabelEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = true;
		}

		private void DeleteLabelEnablementCheck(ref bool aIsEnabled)
		{
			aIsEnabled = SelectedLabel != null;
		}

		private void TfrmLabels_Closed(object sender, System.EventArgs e)
		{
			TeardownEnablements();
		}

		private void pmenLabelList_Popup(object sender, System.EventArgs e)
		{
			SignalChangeDelegate();
		}
	}
}

